#include "Light.h"
#include "DirectX11.h"


Light::Light(void)
{
	cb = nullptr;
}


Light::~Light(void)
{
	SAFE_RELEASE(cb);
}


void Light::ApplyLight()
{
	ID3D11DeviceContext* deviceContext = DirectX11::GetInstance()->GetImmediateContext();
	D3D11_MAPPED_SUBRESOURCE msr;
	deviceContext->Map(cb, NULL, D3D11_MAP_WRITE_DISCARD, 0, &msr);
	memcpy(msr.pData, &bufferData, sizeof(LIGHT));
	deviceContext->Unmap(cb, 0);

	DirectX11::GetInstance()->SetConstantBuffer(&cb,DirectX11::CB_LIGHT);
}

void Light::Initialize(XMFLOAT4 color)
{
	D3D11_BUFFER_DESC desc;
	ZeroMemory(&desc,sizeof(D3D11_BUFFER_DESC));
	desc.Usage = D3D11_USAGE_DYNAMIC;
	desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	desc.ByteWidth = sizeof(LIGHT);
	desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

	D3D11_SUBRESOURCE_DATA init;
	ZeroMemory(&init,sizeof(D3D11_SUBRESOURCE_DATA));
	init.pSysMem = &bufferData;

	HRESULT hr = DirectX11::GetInstance()->GetDevice()->CreateBuffer(&desc,&init,&cb);
	ERRORCHECK(hr,L"ERROR: failed to create a light CB");

	bufferData.color = color;
}

void Light::UpdateLight(float dt)
{
	bufferData.lightPos = XMFLOAT4(sinf(dt*0.3f)*2,5.0f, cosf(dt*0.3f)*2,3.0f);

}




